from activist.Data import Data
from activist.MetaData import MetaData
import mimetypes

def commandCreate(filename,data):
    return Media(filename,data)
    

class Media(Data):
    def __init__(self,expectedid=None):
        super(Media,self).__init__(expectedid)
        self.command("create")
        
    command_create_VERSION = 0
    def command_create(self,filename,data,mimetype=None,creator=None):
        self._createcalled = True
        self._filename = filename
        self._data = data
        self._mimetype = mimetype

        if creator:
            MetaData(self,MetaData.Creator,creator)

    def mimetype(self):
        if self._mimetype is None:
            (ty,encoding) = mimetypes.guess_type(self._filename)
            self._mimetype = encoding
            return encoding
        return self._mimetype

    def data(self):
        return self._data
        
    def getXML(self):
        return """
<media class="activist.Media" id="%(id)s">
  <filename>%(filename)s</filename>
%(meta)s
<data><![CDATA[%(data)s]]></data>
</media>
""" % { "id": self.id(),
        "filename": self._filename,
        "data": self._data,
        "meta":self.getMetaXML()
        }

